package com.chyblog.demo.clients;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateExample {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 使用自己的服务做远程服务名
     */
    @Value("${spring.application.name}")
    private String remoteServiceName;

    public String service() {
        String url = "http://" + remoteServiceName + "/message";
        // use the "smart" Eureka-aware RestTemplate
        ResponseEntity<String> exchange = this.restTemplate.exchange(url, HttpMethod.GET, null, String.class);
        return exchange.getBody();
    }
}
